package com.universal_library.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.view.View;

import com.universal_library.fragment.BaseFragment;

/**
 * Created by solin on 2018/3/14.
 */

public class BaseViewPagerAdapter extends FragmentPagerAdapter {
    private PagerInfo[] mInfolist;
    private BaseFragment mCurfragment;
    private Context mContext;

    public BaseViewPagerAdapter(FragmentManager fm, PagerInfo[] mInfolist, Context mContext) {
        super(fm);
        this.mInfolist = mInfolist;
        this.mContext = mContext;
    }

    @Override
    public void setPrimaryItem(View container, int position, Object object) {
        super.setPrimaryItem(container, position, object);
        if (object instanceof BaseFragment)
            mCurfragment = (BaseFragment) object;
    }

    public Fragment getCurFragment() {
        return mCurfragment;
    }

    @Override
    public Fragment getItem(int position) {
        PagerInfo info = mInfolist[position];
        return BaseFragment.instantiate(mContext, info.clx.getName(), info.args);
    }

    @Override
    public int getCount() {
        return mInfolist.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mInfolist[position].title;
    }

    @Override
    public int getItemPosition(Object object) {
        return PagerAdapter.POSITION_NONE;
    }

    /**
     * 自定义类
     */
    public static class PagerInfo {
        private String title;
        private Class<?> clx;
        private Bundle args;

        public PagerInfo(String title, Class<?> clx, Bundle args) {
            this.title = title;
            this.clx = clx;
            this.args = args;
        }
    }
}
